set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.26)

# specify cross-compilers and tools
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
set(CMAKE_AR arm-none-eabi-ar)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP arm-none-eabi-objdump)
set(SIZE arm-none-eabi-size)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

#set(CMAKE_CXX_COMPILER_LAUNCHER ccache)
#set(CMAKE_C_COMPILER_LAUNCHER ccache)

# project settings
project(air001_template C CXX ASM)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
set(CMAKE_VERBOSE_MAKEFILE ON)

set(MCU -mcpu=cortex-m0plus -mthumb)
set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/project/misc/ldscript.ld)

add_compile_options(${MCU})
add_compile_options(-Wall -fdata-sections -ffunction-sections)
add_compile_options(-fsingle-precision-constant)
add_compile_options(-fno-exceptions)
add_compile_options(-ffreestanding)
# Wno
#add_compile_options(-Wno-missing-braces)
add_compile_options(-Wno-format)

if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
    message(STATUS "Maximum optimization for speed")
    # flto
    add_compile_options(-flto)
    add_compile_options(-Os)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
    message(STATUS "Maximum optimization for speed, debug info included")
    add_compile_options(-Ofast -g)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
    message(STATUS "Maximum optimization for size")
    add_compile_options(-Os)
else ()
    message(STATUS "Minimal optimization, debug info included")
    add_compile_options(-flto)
    add_compile_options(-Og -g -gdwarf-3)
endif ()

include_directories("project/inc"
        "libraries/AIR001xx_HAL_Driver/Inc"
        "libraries/AIR001xx_HAL_Driver/Inc/Legacy"
        "libraries/CMSIS/Device/AIR001xx/Include"
        "libraries/CMSIS/Include"
        "libraries/BSP/AIR001_Core_Board"
        "libraries/Qfp_Fix")

add_definitions(-DAIR001_DEV)

file(GLOB HAL_Lib
        "libraries/AIR001xx_HAL_Driver/Src/*.c"
        "libraries/AIR001xx_HAL_Driver/Src/Legacy/*.c"
)
list(FILTER HAL_Lib EXCLUDE REGEX ".*template\\.c$")

file(GLOB_RECURSE SOURCES
        "project/src/*.c"
        "project/src/startup_air001xx.s"
        "libraries/CMSIS/Device/AIR001xx/Source/system_air001xx.c"
        "libraries/BSP/AIR001_Core_Board/air001_Core_Board.c"
        "libraries/Qfp_Fix/qfp_fix.c"
        "libraries/Qfp_Fix/qfplib-m0-full.S"
        ${HAL_Lib})

add_link_options(${MCU})
add_link_options(-specs=nano.specs -specs=nosys.specs)
add_link_options(-T ${LINKER_SCRIPT})
add_link_options(-lc -lm)
add_link_options(-Wl,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map -Wl,--gc-sections)
#add_link_options(-u _printf_float)

add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})

set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMAND ${SIZE} --format=berkeley $<TARGET_FILE:${PROJECT_NAME}.elf>
        COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")
